const postService = require('../service/post.service')
const {
	POST_CREATE_FAILED,
	POST_UPDATE_FAILED,
	POST_REMOVE_FAILED,
	POST_IS_NOT_EXISTS,
	POST_SELECT_FAILED
} = require('../config/error')

class PostController {
	// 创建帖子
	async create(ctx, next) {
		try {
			const { id } = ctx.user
			const { content, image_url, title } = ctx.request.body
			const result = await postService.create(id, content, image_url, title)
			ctx.body = {
				code: 0,
				message: '创建帖子成功'
			}
		} catch (error) {
			console.log(error)
			ctx.app.emit('error', POST_CREATE_FAILED, ctx)
		}
	}
	// 更新帖子
	async update(ctx, next) {
		try {
			const { id } = ctx.user
			const { post_id } = ctx.params
			const { content, image_url, title } = ctx.request.body
			const result = await postService.update(
				id,
				post_id,
				content,
				image_url,
				title
			)
			ctx.body = {
				code: 0,
				message: '更新帖子成功'
			}
		} catch (error) {
			console.log(error)
			ctx.app.emit('error', POST_UPDATE_FAILED, ctx)
		}
	}

	// 删除帖子
	async remove(ctx, next) {
		try {
			const { post_id } = ctx.params
			// 查找帖子是否存在 存在了才删除
			const postInfo = await postService.findPostById(post_id)
			if (postInfo.length === 0) {
				ctx.app.emit('error', POST_IS_NOT_EXISTS, ctx)
			}
			// 删除帖子
			const result = postService.remove(post_id)
			ctx.body = {
				code: 0,
				message: '删除帖子成功'
			}
		} catch (error) {
			ctx.app.emit('error', POST_REMOVE_FAILED, ctx)
		}
	}

	// 查询帖子
	async findPost(ctx, next) {
		try {
			const { title, name, status, limit, offset } = ctx.query
			const result = await postService.findPost(
				title,
				name,
				status,
				limit,
				offset
			)
			ctx.body = {
				code: 0,
				message: '查询帖子成功',
				data: result
			}
		} catch (error) {
			ctx.app.emit('error', POST_SELECT_FAILED, ctx)
		}
	}

	// 修改帖子的状态
	async updatePostStatus(ctx, next) {
		try {
			const { post_id } = ctx.params
			console.log(post_id)
			const { status } = ctx.query
			const result = await postService.updatePostStatus(post_id, status)
			ctx.body = {
				code: 0,
				message: '修改帖子成功'
			}
		} catch (error) {
			ctx.app.emit('error', POST_UPDATE_FAILED, ctx)
		}
	}

	// 根据id查找帖子
	async findPostById(ctx, next) {
		try {
			const { post_id } = ctx.params
			const result = await postService.findPostById(post_id)
			ctx.body = {
				code: 0,
				message: '查找帖子成功',
				data: result
			}
		} catch (error) {
			console.log(error)
		}
	}

	// 点赞帖子
	async likePost(ctx, next) {
		const { post_id } = ctx.params
		const { id } = ctx.user
		const { isLike } = ctx
		const result = await postService.likeOrCancelLike(post_id, id, isLike)
		ctx.body = {
			code: 0,
			message: result ? '点赞成功' : '取消点赞'
		}
	}

	// 收藏帖子
	async collectePost(ctx, next) {
		const { post_id } = ctx.params
		const { id } = ctx.user
		const { isFavority } = ctx
		const result = await postService.collectionOrCancelCollection(
			post_id,
			id,
			isFavority
		)
		ctx.body = {
			code: 0,
			message: result ? '收藏成功' : '取消收藏'
		}
	}

	// 根据id查看收藏的帖子
	async findPostsById(ctx, next) {
		const { id } = ctx.user
		console.log(id)
		const { limit, offset } = ctx.query

		const result = await postService.queryCollectionPosts(id, limit, offset)
		console.log(result)
		ctx.body = {
			code: 0,
			message: '查看收藏的帖子成功',
			data: result
		}
	}

	// 根据id查询收藏的人
	async findCollectionUserById(ctx, next) {
		const { post_id } = ctx.params
		console.log(post_id)
		const result = await postService.findCollectionUserById(post_id)
		ctx.body = {
			code: 0,
			message: '查看收藏的人成功',
			data: result
		}
	}

	// 根据id查询点赞的人
	async findLikeUserById(ctx, next) {
		const { post_id } = ctx.params
		const result = await postService.findLikeUserById(post_id)
		ctx.body = {
			code: 0,
			message: '查看点赞的人成功',
			data: result
		}
	}

	// 查找我发布的帖子
	async findMinePost(ctx, next) {
		const { id } = ctx.user
		const result = await postService.findMinePost(id)
		ctx.body = {
			code: 0,
			message: '查询我的帖子成功',
			data: result
		}
	}

	//根据用户id查找贴
	async finePostByUserId(ctx, next) {
		const { user_id } = ctx.params
		const result = await postService.finePostByUserId(user_id)
		ctx.body = {
			code: 0,
			message: '查询我的帖子成功',
			data: result
		}
	}
}

module.exports = new PostController()
